#!/bin/bash
do_install_rancherd()
{
    # Check if rancherd exists
    if ! command -v rancherd &> /dev/null
    then
        echo "rancherd not found, starting download..."

        # Set architecture variable
        arch=""
        case "${HOSTTYPE}" in
            arm64|aarch64)
                arch="arm64"
                ;;
            x86_64|amd64)
                arch="amd64"
                ;;
            *)
                echo "Unsupported architecture: $(uname -m)"
                exit 1
                ;;
        esac

        # Download rancherd
        curl -L -o rancherd https://ghproxy.cn/https://github.com/rancher/rancherd/releases/download/v0.3.0-rc1/rancherd-${arch}
        # Add execute permission
        chmod +x rancherd

        # Move to /usr/local/bin
        sudo mv rancherd /usr/local/bin/rancherd

        echo "rancherd has been successfully installed."
    else
        echo "rancherd already exists."
    fi
}

do_config_rancherd_service()
{
    if systemctl list-units --full -all | grep -q 'rancherd.service'; then
        echo "rancherd service already exists"
	# Make sure the service is enabled
        systemctl enable rancherd.service

    else
        echo "rancherd service does not exist, creating service file"
        cat << 'EOF' > /etc/systemd/system/rancherd.service
        [Unit]
        Description=Rancher Bootstrap
        Documentation=https://github.com/rancher/rancherd
        Wants=network-online.target
        After=network-online.target
    
        [Install]
        WantedBy=multi-user.target
    
        [Service]
        Type=oneshot
        EnvironmentFile=-/etc/default/%N
        EnvironmentFile=-/etc/sysconfig/%N
        KillMode=process
        # Having non-zero Limit*s causes performance problems due to accounting overhead
        # in the kernel. We recommend using cgroups to do container-local accounting.
        LimitNOFILE=1048576
        LimitNPROC=infinity
        LimitCORE=infinity
        TasksMax=infinity
        TimeoutStartSec=0
        ExecStart=/usr/local/bin/rancherd bootstrap
EOF
    
        # Reload systemd configuration
        systemctl daemon-reload
    
        # Enable (but not start) the service
        systemctl enable rancherd.service
    fi

}

do_install_yq()
{
    # Check if yq exists
    if ! command -v yq &> /dev/null
    then
        echo "yq command not found, starting download..."

        # Set architecture variable
        arch=""
        case "${HOSTTYPE}" in
            arm64|aarch64)
                arch="arm64"
                ;;
            x86_64|amd64)
                arch="amd64"
                ;;
            *)
                echo "Unsupported architecture: $(uname -m)"
                exit 1
                ;;
        esac

        # Download rancherd
        curl -L -o yq https://ghproxy.cn/https://github.com/mikefarah/yq/releases/download/v4.44.5/yq_linux_${arch}
        # Add execute permission
        chmod +x yq

        # Move to /usr/local/bin
        sudo mv yq /usr/bin/yq

        echo "yq has been successfully installed."
    else
        echo "yq already exists."
    fi
}

do_install_virtctl()
{
    # Check if virtctl exists
    if ! command -v virtctl &> /dev/null
    then
        echo "virtctl command not found, starting download..."

        # Set architecture variable
        arch=""
        case "${HOSTTYPE}" in
            arm64|aarch64)
                arch="arm64"
                ;;
            x86_64|amd64)
                arch="amd64"
                ;;
            *)
                echo "Unsupported architecture: $(uname -m)"
                exit 1
                ;;
        esac

        # Download rancherd
        curl -L -o virtctl https://ghproxy.cn/https://github.com/kubevirt/kubevirt/releases/download/v1.2.2/virtctl-v1.2.2-linux-${arch}
        # Add execute permission
        chmod +x virtctl

        # Move to /usr/local/bin
        sudo mv virtctl /usr/bin/virtctl

        echo "virtctl has been successfully installed."
    else
        echo "virtctl already exists."
    fi
}

do_install_rancherd

do_config_rancherd_service

do_install_yq

do_install_virtctl

